package com.djhu.es.core.request;
/**
 * Created by zw on 2017/12/28.
 */

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.ArrayList;
import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * <p>
 * ---------------------------------
 */
public class BoolSearchRequest extends AbstractRequest implements SearchRequest{



    private List<SearchRequest> searchRequests;

    @Override
    public QueryBuilder queryBuilder() {
        BoolQueryBuilder boolQueryBuilder =   QueryBuilders.boolQuery();
        for (SearchRequest searchRequest:searchRequests){
            if (searchRequest instanceof MustSearchRequest){
                boolQueryBuilder.must(searchRequest.queryBuilder());
            }else if (searchRequest instanceof ShouldSearchRequest){
                boolQueryBuilder.should(searchRequest.queryBuilder());
            }else if (searchRequest instanceof  MustNotSearchRequest){
                boolQueryBuilder.mustNot(searchRequest.queryBuilder());
            }
        }
        return boolQueryBuilder;
    }


    public List<SearchRequest> getSearchRequests() {
        return searchRequests;
    }

    public void setSearchRequests(List<SearchRequest> searchRequests) {
        this.searchRequests = searchRequests;
    }

    public void setSearchRequests(SearchRequest searchRequest){
        if (searchRequests == null){
            searchRequests = new ArrayList<>();
        }
        searchRequests.add(searchRequest);
    }

    public BoolSearchRequest(List<SearchRequest> searchRequests) {
        this.searchRequests = searchRequests;
    }

    public BoolSearchRequest(String index, String type, int timeout, List<SearchRequest> searchRequests) {
        super(index, type, timeout);
        this.searchRequests = searchRequests;
    }
}